package com.wei.duowan.bbs.widget;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.wei.duowan.bbs.ui.adapter.NewsChannelAdapter;

/**
 * 作者：赵若位
 * 时间：2019/10/12 10:55
 * 联系方式：1070138445
 * 功能：
 */
public class NewsChannelTouchHelperCallback extends ItemTouchHelper.Callback
{

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder holder)
    {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        int dragFlags = 0;
        if (adapter instanceof NewsChannelAdapter&&((NewsChannelAdapter) adapter).isDraged(holder.getAdapterPosition()))
        {
            dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.UP | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN;
        }
        return makeMovementFlags(dragFlags, 0);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder from, @NonNull RecyclerView.ViewHolder to)
    {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter instanceof NewsChannelAdapter)
        {
            if (((NewsChannelAdapter) adapter).isSwap(to.getAdapterPosition()))
            {
                ((NewsChannelAdapter) adapter).onItemDraged(from.getAdapterPosition(),to.getAdapterPosition());
                return true;
            }
        }
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i)
    {

    }

}
